package com.example.studyplanning.controller;

import com.example.studyplanning.entity.User;
import com.example.studyplanning.services.UserServices;
import com.example.studyplanning.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserServices userServices;

    //所有用户信息列表
    @GetMapping("/list")
    private Result  FindAllUser(){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(userServices.FindAllUser());
        return result;
    };

    @GetMapping("/lists")
    private Result getUser(@RequestParam("userID") Integer userID){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(userServices.getUser(userID));
        return result;}

    //举报用户
    @PostMapping("/update")
    private Result UpdateUserDislike(@RequestParam("userID") Integer userID){
        Result result = new Result();
        int i = userServices.UpdateUserDislike(userID);
        if(i==1){
            result.setCode(200);
            result.setMessage("更新成功");
        }else{
            result.setCode(400);
            result.setMessage("更新失败");
        }
        return result;
    }

    //撤销举报
    @PostMapping("/Update")
    private Result UpdateDislike(@RequestParam("userID") Integer userID){
        Result result = new Result();
        int i = userServices.UpdateDislike(userID);
        if(i==1){
            result.setCode(200);
            result.setMessage("更新成功");
        }else{
            result.setCode(400);
            result.setMessage("更新失败");
        }
        return result;
    }


    @GetMapping("/getlist/{num}")
    private Result FindUserByName(@PathVariable("num") Integer num,
                                  @RequestParam(value = "userName",defaultValue ="") String userName,
                                  @RequestParam( value = "userID",defaultValue ="0") Integer userID){
        Result result=new Result();
        PageInfo<User> pageInfo = null;
        if (userID==0){
            pageInfo = userServices.findUserByName(userName,num);
        }else{
            pageInfo = userServices.FindUserByName(userName,userID,num);
        }
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(pageInfo);
        return result;
    };

    @GetMapping("/del")
    private Integer DelByID(@RequestParam("userID") Integer userID){
        return  userServices.DelByID(userID);
    };

    //拉黑用户
    @GetMapping("/updateUserType")
    private Result UpdateUserType(@RequestParam("userID") Integer userID){
        Result result = new Result();
        int i = userServices.UpdateUserType(userID);
        if(i==1){
            result.setCode(200);
            result.setMessage("更新成功");
        }else{
            result.setCode(400);
            result.setMessage("更新失败");
        }
        return result;
    }

    //解除拉黑
    @GetMapping("/remove")
    private Result updateUserType(@RequestParam("userID") Integer userID){
        Result result = new Result();
        int i = userServices.updateUserType(userID);
        if(i==1){
            result.setCode(200);
            result.setMessage("更新成功");
        }else{
            result.setCode(400);
            result.setMessage("更新失败");
        }
        return result;
    }

    //用户拉黑选择语句
    @GetMapping("/userlist")
    private Result  FindUser(){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(userServices.FindUser());
        return result;
    };

    @GetMapping("/all")
    public List<User> findAlluser(){
        List<User>list=userServices.findAlluser();
        return list;
    }
    @GetMapping("/active")
    public List<User> findAlActiveuser(){
        List<User>list=userServices.findAllActiveuser();
        return list;
    }

}
